package com.centaline.androidsalesblog.fragments.collection;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.activities.newest.NewEstActDetailActivity;
import com.centaline.androidsalesblog.adapter.NewEstActAdapter;
import com.centaline.androidsalesblog.api.newest.NewEstActListApi;
import com.centaline.androidsalesblog.api.usercenter.CollectInfoListApi;
import com.centaline.androidsalesblog.base.BaseCollectFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.constants.SourceType;
import com.centaline.androidsalesblog.events.MineInfoUpdate;
import com.centaline.androidsalesblog.model.databaseobject.BizUnitDo;
import com.centaline.androidsalesblog.model.databaseobject.NewEstActDo;
import com.centaline.androidsalesblog.model.responseobject.NewEstActListRo;
import com.centaline.androidsalesblog.rx.database.RxDataBase;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IResponseListener;
import com.trello.rxlifecycle.FragmentEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 活动收藏
 * Created by ruanzb on 2016/3/21.
 */
public class ActCollFragment extends BaseCollectFragment<NewEstActDo> {

    private NewEstActListApi newEstActListApi;
    private NewEstActAdapter newEstActAdapter;

    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    @Override
    protected void initView() {
        super.initView();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void initApi(CollectInfoListApi collectInfoListApi) {
        collectInfoListApi.setSourceType(SourceType.HUODONG);
    }

    @Override
    protected void initOtherApi() {
        newEstActListApi = new NewEstActListApi(ActCollFragment.class, NewEstActListRo.class,
                new IResponseListener<NewEstActListRo>() {
                    @Override
                    public void onResponse(NewEstActListRo newEstActListRo) {
                        stopRefresh(newEstActListRo.getList());
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mRecyclerView.refreshError();
                    }
                });
        newEstActListApi.setUsedActIds(1);
    }

    /**
     * 用户中心数据变化
     */
    @Subscribe
    public void onEventMainThread(MineInfoUpdate mineInfoUpdate) {
        int status = mineInfoUpdate.getStatus() / 10;
        if (status == 1) {
            if (mineInfoUpdate.getStatus() == 15) {
                mRecyclerView.startRefresh();
            }
        }
    }

    @Override
    protected void requestList(String ids) {
        newEstActListApi.setActIds(ids);
        newEstActListApi.startRequest();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        newEstActAdapter = new NewEstActAdapter(getActivity(), GlideLoad.init(this), list);
        RxDataBase.getBizUnit()
                .compose(this.<BizUnitDo>bindUntilEvent(FragmentEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BizUnitDo>() {
                    @Override
                    public void call(BizUnitDo bizUnitDo) {
                        newEstActAdapter.setNewPropImg(bizUnitDo.getNewpropimg());
                    }
                });
        return newEstActAdapter;
    }

    @Override
    protected void ItemClick(NewEstActDo newEstActDo) {
        startActivity(new Intent(getContext(), NewEstActDetailActivity.class)
                .putExtra(ExtraConstant.NewEstActId, newEstActDo.getActId()));
    }
}
